const filter = require('../utils/filters');
/**
 * 共用函数
 */
module.exports = {
    /**
     * 随机字符串
     * @param {*} len 数组长度
     * @returns 
     */
    randomString(len) {
        len = len || 32;
        var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnoprstuvwxyz1234567890'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length;
        var pwd = '';
        for (let i = 0; i < len; i++) {
            pwd += $chars.charAt(this.randomNum(maxPos));
        }
        return pwd;
    },

    /**
     * 随机颜色
     * @param {*} min 最小值
     * @param {*} max 最大值
     */
    randomColor(min, max) {
        var number = (max - min) + min;
        var r = this.randomNum(number);
        var g = this.randomNum(number);
        var b = this.randomNum(number);
        var a = 0.1;

        return `rgba(${r},${g},${b},${a})`;
    },

    /**
     * 随机数
     * @param {*} number 
     * @returns 
     */
    randomNum(number) {
        return Math.floor(Math.random() * number);
    },

    /**
     * 处理tabs,分别获取不同状态的数据数量
     * @param {Array} articles 文章列表
     * @param {Array} tabs tabs数组
     * @returns 
     */
    getTabs(articles, tabs = []) {
        let Array = {};

        articles.forEach((v) => {
            if (v.status === 0) {
                (Array[0] || (Array[0] = [])).push(v)
            } else {
                (Array[v.status] || (Array[v.status] = [])).push(v)
            }
        });

        for (let i in Array) {
            tabs.push({ status: Number(i), total: Array[i].length })
        };

        tabs.push({ status: 0, total: articles.length });

        return tabs
    },

    /**
     * 树形数据处理索引0为一级，其他均为子级
     * @param {Array} commentData 列表数据
     * @returns {Array} 
     */
    getData(data, dataArr = []) {
        let newData = data;
        newData.forEach((child) => {
            child.content = decodeURI(child.content)
            data.forEach((parent) => {
                if (child.reply_comment_id == parent.id) {
                    child.reply_account = parent.account;
                    child.reply_name = parent.name || parent.user_name;
                    child.reply_role = parent.role;
                    child.reply_role_name = parent.role_name;
                    child.reply_content = parent.content;
                    child.reply_role_name = parent.role_name;
                }
            })
            if (child.parent_comment_id == 0) {
                (dataArr[0] || (dataArr[0] = [])).push(child);
            } else {
                (
                    dataArr[child.parent_comment_id] ||
                    (dataArr[child.parent_comment_id] = [])
                ).push(child);
            }
        });

        return dataArr;
    },

    /**
     * 处理children数据结构的菜单数据
     * @param {array} menuData 表格数据
     * @param {array} arr 空数组，用于存值
     * @return {array}
     */
    getChildrenTree(dataArr, arr) {
        for (let i in arr) {
            // 如果查找的子类存在就遍历
            let pidArr = dataArr[arr[i].id];

            if (pidArr != undefined) {
                for (let j in pidArr) {

                    (arr[i].childrens || (arr[i].childrens = [])).push(
                        pidArr[j]
                    );
                }

                let getChildrens = this.getChildrenTree(
                    dataArr,
                    arr[i].childrens
                );

                arr[i].childrens = getChildrens;
            }

        }

        return arr;
    },


    /**
     * 数据分页
     * @param {Array} datas 数组
     * @param {Number} per_page 每页多少条
     * @param {Number} page 第几页
     * @returns {Array}
     */
    getPagination(datas, per_page, page) {
        let currentPage = page - 1;

        let showData = datas.filter((item, index) => {
            let pageSize = Math.floor(index / per_page);
            return pageSize == currentPage;
        })

        return showData
    },

    /**
     * 获取最近的日期
     * @param {Number} days 要获取的日期天数
     * @param {String} status date(日期), time(时间戳) 
     * @param {Array} dateArray 空数组
     * @return {Array} 
     */
    getDate(days, dateArray = []) {
        let myDate = new Date(); //获取今天日期
        myDate.setDate(myDate.getDate() - (days - 1));

        let dateTemp;
        let flag = 1;
        for (let i = 0; i < days; i++) {
            dateTemp = myDate.getFullYear() + "-" +
                (myDate.getMonth() + 1).toString().padStart(2, '0') + "-" +
                (myDate.getDate()).toString().padStart(2, '0');

            dateArray.push(dateTemp);
            myDate.setDate(myDate.getDate() + flag);
        }

        return dateArray
    },

    getChartDispose(days, datas) {
        let data = [];
        let newData = {};

        days.forEach((i, index) => {
            newData[i] || (newData[i] = [])
            datas.forEach(v => {
                let date = filter.formatTime(v.created_time);

                if (i == date) {
                    newData[i].push(v)
                }
            })

            let arr = newData[i];
            data.push(arr.length)
        })

        return data;
    }
}